const { ccclass, property } = cc._decorator;
import WinNodeBase from '../WinNodeBase';

@ccclass
export default class levelChoiceControl extends WinNodeBase {
    @property(cc.Node)
    checked: cc.Node = null;
    @property(cc.Node)
    page_1: cc.Node = null;
    @property(cc.Node)
    page_2: cc.Node = null;
    @property(cc.Node)
    page_3: cc.Node = null;
    @property(cc.Prefab)
    _itemPrefab: cc.Prefab = null;
    @property(cc.Node)
    _checkNode: cc.Node = null;
    _gameHelper: gameHelper = null;
    private yList: number[] = [];
    private xList: number[] = [];
    // selectEvents: Array<cc.Component.EventHandler> = [];
    onLoad() {
        this._gameHelper = gameHelper.getInstance();
        this.initPage();
    }
    initPage() {
        let ll = gameHelper.getInstance().levelLength;
        for (let i = 1; i <= ll; i++) {
            let node = cc.instantiate(this._itemPrefab);
            node.on(cc.Node.EventType.TOUCH_START, this.clickItem, this);
            node.name = i.toString();
            if (this.getlevel().length >= i) {
                node.getChildByName("level").getComponent(cc.Label).string = node.name;
                let _star = node.getChildByName("star");
                for (let iy = 0; iy < this.getlevel()[i - 1]; iy++) {
                    if (iy > 2)
                        break;
                    _star.children[iy].active = true;
                }
            }
            else {
                node.getChildByName("lock").active = true;
                node.getChildByName("star").active = false;
            }
            this.addItem(i - 1, node);
            if (i == this._gameHelper.selectLevelId) {
                this.checked.parent = node;
                this.checked.active = true;
            }
        }
    }
    setxyList(node: cc.Node) {
        let offset: number = 40;
        let offsety1 = (this.page_1.width - offset * 2 - node.width * 4) / 3;
        this.xList[0] = (offsety1 + node.width) * -1.5;
        this.xList[1] = (offsety1 + node.width) * -0.5;
        this.xList[2] = 0 - this.xList[1];
        this.xList[3] = 0 - this.xList[0];

        this.yList[0] = (this.page_1.height / 2 - node.height / 2) - offset;
        this.yList[1] = 0;
        this.yList[2] = 0 - this.yList[0];
    }
    addItem(index: number, node: cc.Node) {
        if (this.yList.length == 0) {
            this.setxyList(node);
        }
        let page = Math.floor(index / 12);
        let pageIndex = index - page * 12;
        let line = Math.floor(pageIndex / 4);
        let column = pageIndex - line * 4;

        node.x = this.xList[column];
        node.y = this.yList[line];
        switch (page) {
            case 0:
                this.page_1.addChild(node);
                break;
            case 1:
                this.page_2.addChild(node);
                break;
            default:
                this.page_3.addChild(node);
                break;
        }
    }
    clickItem(event: cc.Event.EventCustom) {
        let id: number = event.target.name;
        if (this.getlevel().length < id)
            return;
        this._gameHelper.selectLevelId = id;
        this.checked.parent = event.target;
        // cc.Component.EventHandler.emitEvents(this.selectEvents, {
        //     target: this,
        //     index: event.target.name
        // });
    }
    getlevel(): any {
        let date = this._gameHelper.levelParam;
        if (!date || date.length == 0) {
            this._gameHelper.levelParam = [0];
        }
        return this._gameHelper.levelParam;
    }
}
